﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;

using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Sunboxsoft.Common;
using DevExpress.XtraEditors;
using Sunboxsoft.ASMS.Client.UI.Properties;

namespace Sunboxsoft.ASMS.Client.UI.Management
{
    public partial class ServiceCategoryListView : UserControl, IView
    {
        private int _pageIndex;
        private int _pageCount;

        public ServiceCategoryListView()
        {
            InitializeComponent();
        }

        #region override
        protected override void OnLoad(EventArgs e)
        {
           LookUpEditUtil.BindIntCodeTextItem(ctlLookUpStatus, Domain.Constant.CommonStatus.Enumerate(), Domain.Constant.CommonStatus.AllItem);
            LoadServiceCategoryData();
            base.OnLoad(e);
        }
        #endregion


        #region IView 成员

        public void RegisterAction(ActionNodeCollection collection)
        {
            collection.AddAction(new ActionNode("create", Resources.isAdd, "新建", OnCreateActionClick))
                .AddAction(new ActionNode("refresh", Resources.isRefresh, "刷新", OnRefreshActionClick));
        }
        public void Resume(ViewResumeArgs args)
        {
        }
        public void Active(ViewActiveArgs args)
        {
           
        }
        public ViewHeader GetHeader()
        {
            return new ViewHeader("");
        }
        #endregion

        #region actions

        private void OnCreateActionClick(object sender, object e)
        {
        }

        private void OnRefreshActionClick(object sender, object e)
        {
            LoadServiceCategoryData();
        }

        #endregion

        #region methods
        private void LoadServiceCategoryData()
        {
            this.ShowMask("Loading...");
            UIContext.Current.InvokeAsync(() =>
            {
                PagedResult<WebRef.EServiceCategory> result = new Service.QueryService().QueryServiceCategoryPagedList(ModuleContext.Current.BusinessId,
                    ModuleContext.Current.StationId,
                    GetStatus(),_pageIndex * GetPageLimit(), GetPageLimit());
                this.BeginInvoke(new Action(() =>
                {
                    if (result.IsSuccess)
                    {
                        ctlGrid.Paginate<WebRef.EServiceCategory>(result, out _pageIndex, out _pageCount);
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError(result.Message);
                    }
                    this.HideMask();
                }));
            });
        }

        private int GetPageLimit()
        {
            return 25;
        }

        private int GetStatus()
        {
            Domain.IntCodeTextItem selectedItem = ctlLookUpStatus.GetSelectedDataRow() as Domain.IntCodeTextItem;
            if (selectedItem == null)
            {
                return Domain.Constant.CommonStatus.ALL;
            }
            return selectedItem.Code;
        }
        #endregion

        private void ctlGrid_EmbeddedNavigator_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            e.UpdatePageIndex(_pageCount, ref _pageIndex);
            LoadServiceCategoryData();
        }
    }
}
